# encoding: utf-8

from zheezes.po.ip_addr import IpAddr 
from zheezes.util.ip2addr import qq_ip_to_addr

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainPage(webapp.RequestHandler):
    def get(self):
        ip = self.request.get("ip")
        rs = IpAddr.gql("WHERE ip = :ip LIMIT 1", ip=ip);
        ipaddr = rs.get()
        if ipaddr:
            addr = ipaddr.addr
            src = "db"
        else:
            addr = qq_ip_to_addr(ip)
            src= "qq"
        #print username
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('[%s]: %s - %s' % (src, ip, addr))
        #self.response.out.write(username)
        
application = webapp.WSGIApplication([('/.*', MainPage)], debug=True)

def main():
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
